package com.itheima.service.omsService.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.commons.PageBean;
import com.itheima.domain.db.oms.OmsOrderReturnApply;
import com.itheima.dto.OmsOrderReturnApplyDto;
import com.itheima.mapper.omsMapper.OrderReturnApplyMapper;
import com.itheima.service.omsService.OrderReturnApplyService;
import com.itheima.util.TimeUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 退货申请
 *
 * @author DogeGuo
 */
@DubboService
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyMapper, OmsOrderReturnApply> implements OrderReturnApplyService {

    @Autowired
    OrderReturnApplyMapper mapper;

    /**
     * 根据条件分页查询
     *
     * @param dto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<OmsOrderReturnApply> pageReturnWithParams(OmsOrderReturnApplyDto dto, Integer pageNum, Integer pageSize) {
        Page<OmsOrderReturnApply> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<OmsOrderReturnApply> wrapper = new LambdaQueryWrapper();

        if (dto != null) {
            //申请时间
            if (dto.getCreatTime() != null) {
                wrapper.eq(OmsOrderReturnApply::getCreateTime, TimeUtil.getTimeForStr(dto.getCreatTime()));
            }
            //处理人员
            if (dto.getHandleMan() != null) {
                wrapper.eq(OmsOrderReturnApply::getHandleMan, dto.getHandleMan());
            }
            //处理时间
            if (dto.getHandleTime() != null) {
                wrapper.eq(OmsOrderReturnApply::getHandleTime, TimeUtil.getTimeForStr(dto.getHandleTime()));
            }
            //服务单号
            if (dto.getId() != null) {
                wrapper.eq(OmsOrderReturnApply::getId, Convert.toInt(dto.getId()));
            }
            //申请状态
            if (dto.getStatus() != null) {
                wrapper.eq(OmsOrderReturnApply::getStatus, dto.getStatus());
            }

            //收货人姓名/号码
            //收货人姓名
            if (dto.getReceiverKeyword() != null) {
                wrapper.like(OmsOrderReturnApply::getReceiveMan, dto.getReceiverKeyword())
                        //退货人电话
                        .or().like(OmsOrderReturnApply::getReturnPhone, dto.getReceiverKeyword());
            }
        }

        mapper.selectPage(page, wrapper);

        PageBean<OmsOrderReturnApply> pageBean = new PageBean(page);

        return pageBean;
    }

    /**
     * 根据id获取详情
     *
     * @param id
     * @return
     */
    @Override
    public OmsOrderReturnApply getInfoById(Integer id) {
        OmsOrderReturnApply omsOrderReturnApply = mapper.selectById(id);
        return omsOrderReturnApply;
    }

    /**
     * 退货状态修改
     *
     * @param omsOrderReturnApply
     */
    @Override
    public void updateStatusById(OmsOrderReturnApply omsOrderReturnApply) {
        mapper.updateById(omsOrderReturnApply);
    }

    /**
     * 批量删除退货申请
     *
     * @param ids
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            mapper.deleteById(id);
        }
    }
}
